package com.wll.ec.fast.main.personal.list;

import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.SwitchCompat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wll.ec.fast.R;
import com.wll.ec.fast.main.personal.PersonalItemType;
import com.wll.ec.fast.main.personal.profile.UserProfileFields;

import java.util.List;

/**
 * created by detachment on 2020/3/26
 */
public class ListAdapter extends BaseMultiItemQuickAdapter<ListBean, BaseViewHolder> {

    private static final RequestOptions OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .dontAnimate()
            .centerCrop();

    public ListAdapter(List<ListBean> data) {
        super(data);
        addItemType(PersonalItemType.ITEM_NORMAL, R.layout.arrow_item_layout);
        addItemType(PersonalItemType.ITEM_AVATAR, R.layout.arrow_item_avatar);
        addItemType(PersonalItemType.ITEM_SWITCH, R.layout.arrow_switch_layout);
    }

    public void updateUserProfile(String response){
        final JSONObject profileJson = JSON.parseObject(response).getJSONObject("data");
        final long userId = profileJson.getLong("userId");
        final String name = profileJson.getString("name");
        final String avatar = profileJson.getString("avatar");
        final String gender = profileJson.getString("gender");
        final String address = profileJson.getString("address");
        final String birthday = profileJson.getString("birthday");

        if (this.getData() != null) {
            int size = getData().size();
            for (int i = 0; i < size; i++) {
                int id = getData().get(i).getId();
                if (id == UserProfileFields.ID){
                    getData().get(i).setImageUrl(avatar);
                } else if (id == UserProfileFields.NAME){
                    getData().get(i).setValue(name);
                }else if (id == UserProfileFields.GENDER){
                    getData().get(i).setValue(gender);
                }else if (id == UserProfileFields.BIRTHDAY){
                    getData().get(i).setValue(birthday);
                }
            }
        }
        notifyItemChanged(0, "arrow_avatar");
        notifyItemRangeChanged(1, getItemCount(), "arrow_value");
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position, @NonNull List<Object> payloads) {
        if (payloads == null || payloads.isEmpty()) {
            this.onBindViewHolder(holder, position);
        } else {
            String payload = (String) payloads.get(0);
            switch (holder.getItemViewType()) {
                case PersonalItemType.ITEM_NORMAL:
                    if (payload.equals("arrow_value")) {
                        ListBean bean = this.getData().get(position);
                        String value = bean.getValue();
                        if (value != null && !value.isEmpty()) {
                            holder.setText(R.id.tv_arrow_value, value);
                        }
                    }
                    break;
                case PersonalItemType.ITEM_AVATAR:
                    if (payload.equals("arrow_avatar")) {
                        ListBean bean = this.getData().get(position);
                        String avatar = bean.getImageUrl();
                        if (avatar != null && !avatar.isEmpty()) {
                            Glide.with(mContext)
                                    .load(avatar)
                                    .apply(OPTIONS)
                                    .into((ImageView) holder.getView(R.id.img_arrow_avatar));
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, ListBean item) {
        switch (helper.getItemViewType()) {
            case PersonalItemType.ITEM_NORMAL:
                helper.setText(R.id.tv_arrow_text, item.getText());
                helper.setText(R.id.tv_arrow_value, item.getValue());
                break;
            case PersonalItemType.ITEM_AVATAR:
                Glide.with(mContext)
                        .load(item.getImageUrl())
                        .apply(OPTIONS)
                        .into((ImageView) helper.getView(R.id.img_arrow_avatar));
                break;
            case PersonalItemType.ITEM_SWITCH:
                helper.setText(R.id.tv_arrow_switch_text, item.getText());
                final SwitchCompat switchCompat = helper.getView(R.id.list_item_switch);
                switchCompat.setChecked(true);
                switchCompat.setOnCheckedChangeListener(item.getmOnCheckedChangeListener());
                break;
            default:
                break;
        }
    }
}
